{% extends "base_index.html" %}
{% load commonFilters %}
{% load i18n %}

{% comment %}
copyright (c) 2009, paketka@gmail.com et. al
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

	* Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.

	* Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.

	* Neither the name of the <ORGANIZATION> nor the names of its contributors
	  may be used to endorse or promote products derived from this software
 	  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{% endcomment %}

{% block rss-link %}
<link rel="alternate" type="application/rss+xml" title="RSS-poslanci" href="{% url rss_link "hlasovani" %}" />
{% endblock %}

{% block keywords %}
analýza výsledků hlasování, výsledky hlasování, přehled výsledků,
{% ifnotequal poll.name "Procedurální hlasování" %} {{ poll.name }} {% endifnotequal %},
hlasování dne {{ poll.date|date:"Y m d" }}, číslo hlasování {{ poll.pollId }}
{% endblock %}
{% block js-links %}
	{{ block.super }}
	<script type="text/javascript" src="{{ MEDIA_URL }}/js/jquery-ui-1.7.custom.min.js"></script>
	<script type="text/javascript" src="{{ MEDIA_URL }}/js/ui.accordion.js"></script>
	<script type="text/javascript" src="{{ MEDIA_URL }}/js/jquery.scrollTo-min.js"></script>
{% endblock %}

{% block javascript-head %}

function showPie(groupId, jsonLinkCharts, jsonLinkGrid, title, gridConstructor)
{
	pieElemId = '#pie_' + groupId;
	pieCtrlId = '#pieCtrl_' + groupId;

	if ($(pieElemId).children('p').children('#progressImg_' + groupId).length == 1) {
		$(pieElemId).slideDown('fast');
		$.ajax({
				url : jsonLinkCharts,
				async : true,
				dataType : 'json',
				success : function(data) {
						$(pieElemId).children('p').text('')
						$(pieElemId).children('p').html(
								data.pollPie + ' ' + data.partyPie +
								'<table id="pollDashBd_' + groupId + '" class="scroll" cellpadding="0" cellspacing="0"></table>' +
								'<div id="pollDashBd_' + groupId + '_meetingPager' + '" class="scroll" style="text-align:center;"></div>'
						);
						gridConstructor(groupId, title, jsonLinkGrid);
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
						$(pieElemId).html('<img src="{{ MEDIA_URL }}/images/error.gif"/>');
				}
		});
	}
}

function createPartyGrid(groupId, title, link)
{
	$("#pollDashBd_" + groupId).jqGrid({
				url: link,
				datatype: 'json',
				colNames: [	"{% trans "Jméno" %}",
							"{% trans "Příjmení" %}",
							"{% trans "Volební kraj" %}",
							"{% trans "Hlas" %}"
					],
				colModel :[
					{ name: "name", index: "name", width: 120, jsonmap: "name", sortable: true },
					{ name: "surname", index: "surname", width:120, jsonmap:"surname", sortable: true },
					{ name: "area", index: "area", width: 120, jsonmap: "area", sortable: true  },
					{ name: "result", index: "result", width: 120, jsonmap: "result", sortable: true  }
				], 
				pager: $('#pollDashBd_' + groupId + '_meetingPager'),
				jsonReader: {
					root: "rows",
					id: "pk",
					page: "page",
					total: "total",
					records: "records",
					repeatitems: false
				},
				sortname:"surname",
				sortorder:"asc",
				rowNum:10,
				rowList:[10,15,20],
				viewrecords:true,
				imgpath:"{{ MEDIA_URL }}/css/jqgrid/themes/basic/images",
				caption: '{% trans "Hlasování" %}' + title,
				width:480,
				toolbar: [true,'top']
	});
	$("#t_" + "pollDashBd_" + groupId).filterGrid("#pollDashBd_" + groupId, {
				gridModel: false,
				gridToolBar: false,
				filterModel: [ {
					label: '',
					name: 'filter__exp',
					stype: 'text',
					defval: ''
				} ],
				autosearch: true,
				searchButton: '{% trans "Hledej" %}',
				enableSearch: true
			}
		);
}

function createGroupGrid(groupId, title, link)
{
	$("#pollDashBd_" + groupId).jqGrid({
				url: link,
				datatype: 'json',
				colNames: [	"{% trans "Jméno" %}",
							"{% trans "Příjmení" %}",
							"{% trans "Poslanecký klub" %}",
							"{% trans "Volební kraj" %}",
							"{% trans "Hlas" %}"
					],
				colModel :[
					{ name: "name", index: "name", width: 120, jsonmap: "name", sortable: true },
					{ name: "surname", index: "surname", width:120, jsonmap:"surname", sortable: true },
					{ name: "party", index: "party", width: 120, jsonmap:"party", sortable: true },
					{ name: "area", index: "area", width: 120, jsonmap: "area", sortable: true  },
					{ name: "result", index: "result", width: 120, jsonmap: "result", sortable: true  }
				], 
				pager: $('#pollDashBd_' + groupId + '_meetingPager'),
				jsonReader: {
					root: "rows",
					id: "pk",
					page: "page",
					total: "total",
					records: "records",
					repeatitems: false
				},
				sortname:"surname",
				sortorder:"asc",
				rowNum:10,
				rowList:[10,15,20],
				viewrecords:true,
				imgpath:"{{ MEDIA_URL }}/js/themes/basic/images",
				caption: title,
				width:600,
				toolbar: [true,'top']
	});
	$("#t_" + "pollDashBd_" + groupId).filterGrid("#pollDashBd_" + groupId, {
				gridModel: false,
				gridToolBar: false,
				filterModel: [ {
					label: '',
					name: 'filter__exp',
					stype: 'text',
					defval: ''
				} ],
				autosearch: true,
				searchButton: '{% trans "Hledej" %}',
				enableSearch: true
			}
		);
}

function accordionChange(event, ui) {
	pieCtrl = ui.newHeader.children('[onclick]');
	if (pieCtrl.length != 0) {
		callShowPie = pieCtrl.attr('onclick');
		if (callShowPie != null)
			callShowPie();
	}
}

$(document).ready(function(){
	createGroupGrid(
		'parlament',
		'parlament',
		'{% url get_polls_in_group parlament.id, poll.id %}'
	);

	$("#main-accordion").taccordion({
		collapsible: true,
		autoHeight: false,
		clearStyle: true,
		navigation: true,
		header: 'h2',
		change: accordionChange
	});

	$("#clubs-accordion").taccordion({
		collapsible: true,
		autoHeight: false,
		clearStyle: true,
		navigation: true,
		header: 'h3',
		change: accordionChange
	});
	$("#clubs-accordion").taccordion('activate', -1);

	$("#boards-accordion").taccordion({
		collapsible: true,
		autoHeight: false,
		clearStyle: true,
		navigation: true,
		header: 'h3',
		change: accordionChange
	});
	$("#boards-accordion").taccordion('activate', -1);

	{% for board in boards %}
		{% if board.subGroups %}
		{% if board.subGroups|length %}
			$("#subboards-accordion_{{ board.id }}").taccordion({
				collapsible: true,
				autoHeight: false,
				clearStyle: true,
				navigation: true,
				header: 'h4',
				change: accordionChange
			});

			$("#subboards-accordion_{{ board.id }}").taccordion('activate', -1);
		{% endif %}
		{% endif %}
	{% endfor %}

	$("#delegs-accordion").taccordion({
		collapsible: true,
		autoHeight: false,
		clearStyle: true,
		navigation: true,
		header: 'h3',
		change: accordionChange
	});
	$("#delegs-accordion").taccordion('activate', -1);

	$("#comms-accordion").taccordion({
		collapsible: true,
		autoHeight: false,
		clearStyle: true,
		navigation: true,
		header: 'h3',
		change: accordionChange
	});
	$("#comms-accordion").taccordion('activate', -1);

	$("#mpu-accordion").taccordion({
		collapsible: true,
		autoHeight: false,
		clearStyle: true,
		navigation: true,
		header: 'h3',
		change: accordionChange
	});
	$("#mpu-accordion").taccordion('activate', -1);

	$("#other-accordion").taccordion({
		collapsible: true,
		autoHeight: false,
		clearStyle: true,
		navigation: true,
		header: 'h3',
		change: accordionChange
	});
	$("#other-accordion").taccordion('activate', -1);

	{% if clickElem %}
		$("#{{ clickElem }}").trigger("click");
		$.scrollTo("#{{ clickElem }}", 800);
	{% else %}
		$("#main-accordion").taccordion('activate', -1);
	{% endif %}
});
{% endblock %}

{% block left-sidebar %}
	<div class="ui-widget">
		<div class="ui-widget-header">
			<h1>{% trans "Kalendář hlasování" %}</h1>
		</div>
		<div class="ui-widget-content">
			<div class="container">
				{% url get_meetings_in_month year,month as events %}
				{% url psp.views.show_day_polls year,month,poll.date.day as eventDetails %}
				{% datepicker "meetingPicker" events eventDetails year month %}
			</div>
		</div>
	</div>
{% endblock %}

{% block content %}
<div class="ui-widget">
	<div class="ui-widget-header">
	{% if poll.name %}
		<h1> {{ poll.name }} [ {{ poll.verdict }} ]</h1>
	{% else %}
		<h1> Hlasování bez názvu </h1>
	{% endif %}
	</div>
	<div class="ui-widget-content">
		<div class="left">
		<p>
		{% blocktrans with poll.date as d and poll.resultsLink as results and poll.verdict as v %}
			Hlasování se konalo dne {{ d }}, oficiální přehled lze najít na stránkách <a href="{{ results }}">parlamentu</a>.
			Hlasování skončilo s výsledkem <strong>{{ v }}</strong>.
		{% endblocktrans %}
			{% if poll.stenoLink %}
			{% blocktrans with poll.stenoLink as s %}
				Stenografický záznam rozpravy předcházející hlasování najdete <a href="{{ s }}">zde</a>.
			{% endblocktrans %}
			{% endif %}
		</p>
		</div>
		<div class="right">
			{{ pie.img|safe }}
		</div>
		<div class="clear"></div>
		<div>
			<table id="pollDashBd_parlament" class="scroll" cellpadding="0" cellspacing="0"></table>
			<div id="pollDashBd_parlament_meetingPager" class="scroll" style="text-align:center;"></div>
		</div>
	</div>
</div>

{% url psp.views.show_poll poll.id as baseUrl %}
<div id="main-accordion">
{% if partyClubs|length %}
	<h2><a href="#void_partyClubs" id="sliderCtrl_clubs" > {% trans "Přehledy podle partajních klubů" %} </a> </h2>
	<div id="clubs-accordion">
	{% for party in partyClubs %}
		{% if party.stat %}
			<h3><a
					href="#void_partyGraphs"
					id="pieCtrl_{{ party.id }}"
					onClick="showPie(
								'{{ party.id }}',
								'{% url get_group_poll_json party.id, poll.id %}',
								'{% url get_polls_in_group party.id, poll.id %}',
								'{{ party.getPartyName }}',
								createPartyGrid
							)"
				>
					{{ party.getPartyName }} <span class="verdict">[ {{ party.stat.getResult }} ]</span>
				</a>
			</h3>
			<div id="pie_{{ party.id }}" style="display: none;">
				<a href="{{ baseUrl }}?mainAccordIndex=0&clickElem=pieCtrl_{{ party.id }}">link</a>
				<p><img src="{{ MEDIA_URL }}/images/wait.gif" id="progressImg_{{ party.id }}" alt="Nahrávám graf"/></p>
			</div>
		{% endif %}
	{% endfor %}
	</div>
{% endif %}

{% if boards|length %}
	<h2><a
			href="#void_boards"
			id="sliderCtrl_boards"
		>
			{% trans "Grafy podle výborů" %}
		</a>
	</h2>
	<div id="boards-accordion"> <!--beginning of boards slot -->
	{% for board in boards %}
		{% if board.stat %}
			<h3><a	href="#void_boardGraphs"
					id="pieCtrl_{{ board.id }}"
					onClick="showPie(	'{{ board.id }}',
										'{% url get_group_poll_json board.id, poll.id %}',
										'{% url get_polls_in_group board.id, poll.id %}',
										'{{ board.name }}',
										createGroupGrid
									)"
				>
					{{ board.name }} <span class="verdict">[ {{ board.stat.getResult }} ]</span>
			</a></h3>
			<div id="pie_{{ board.id }}" style="display:none">
				<a href="{{ baseUrl }}?mainAccordIndex=1&clickElem=pieCtrl_{{ board.id }}">link</a>
				<p><img src="{{ MEDIA_URL }}/images/wait.gif" id="progressImg_{{ board.id }}" alt="Nahrávám graf"/></p>

				{% if board.subGroups %}
				{% if board.subGroups|length %}
				{% with board.subGroups as subBoards %}
					<div id="subboards-accordion_{{ board.id }}"> <!-- beginning of subboards slot -->

					{% for subBoard in subBoards %}
						{% if subBoard.stat %}
							<h4><a	href="#void_subboards"
									id="pieCtrl_{{ subBoard.id }}"
									onClick="showPie(	'{{ subBoard.id }}',
														'{% url get_group_poll_json subBoard.id, poll.id %}',
														'{% url get_polls_in_group subBoard.id, poll.id %}',
														'{{ subBoard.name }}',
														createGroupGrid
													)"
								>
									{{ subBoard.name }} <span class="verdict">[ {{ subBoard.stat.getResult }} ]</span>
							</a></h4>
							<div id="pie_{{ subBoard.id }}" style="display:none">
								<p><img src="{{ MEDIA_URL }}/images/wait.gif" id="progressImg_{{ subBoard.id }}" alt="Nahrávám graf"/></p>
							</div>
						{% endif %}
					{% endfor %} 

					</div> <!-- End of subboards slot -->
				{% endwith %}
				{% endif %}
				{% endif %}
			</div> <!-- End of board slot -->
		{% endif %}
	{% endfor %}
	</div> <!--End of boards slot -->
{% endif %}

{% if delegs|length %}
	<h2><a	href="#void_Delegs"
			id="sliderCtrl_delegs"
		>
		{% trans "Grafy podle delegací" %}
	</a></h2>
	<div id="delegs-accordion">
	{% for deleg in delegs %}
		{% if deleg.stat %}
			<h3><a	href="#void_delegGraphs"
					id="pieCtrl_{{ deleg.id }}"
					onClick="showPie(	'{{ deleg.id }}',
										'{% url get_group_poll_json deleg.id, poll.id %}',
										'{% url get_polls_in_group deleg.id, poll.id %}',
										'{{ deleg.name }}',
										createGroupGrid
									)"
				>
					{{ deleg.name }} <span class="verdict">[ {{ deleg.stat.getResult }} ]</span>
				</a>
			</h3>
			<div id="pie_{{ deleg.id }}" style="display: none;">
				<a href="{{ baseUrl }}?mainAccordIndex=2&clickElem=pieCtrl_{{ deleg.id }}">link</a>
				<p><img src="{{ MEDIA_URL }}/images/wait.gif" id="progressImg_{{ deleg.id }}" alt="Nahrávám graf"/></p>
			</div>
		{% endif %}
	{% endfor %}
	</div>
{% endif %}

{% if committees|length %}
	<h2><a	href="#void_committees"
			id="sliderCtrl_comms"
		>
			{% trans "Grafy podle komisí" %}
		</a>
	</h2>
	<div id="comms-accordion">
	{% for comm in committees %}
		{% if comm.stat %}
			<h3><a	href="#void_commGraphs"
					id="pieCtrl_{{ comm.id }}"
					onClick="showPie(	'{{ comm.id }}',
										'{% url get_group_poll_json comm.id, poll.id %}',
										'{% url get_polls_in_group comm.id, poll.id %}',
										'{{ comm.name }}',
										createGroupGrid
									)"
				>
					{{ comm.name }} <span class="verdict">[ {{ comm.stat.getResult }} ]</span>
				</a>
			</h3>
			<div id="pie_{{ comm.id }}" style="display: none;">
				<a href="{{ baseUrl }}?mainAccordIndex=3&clickElem=pieCtrl_{{ comm.id }}">link</a>
				<p><img src="{{ MEDIA_URL }}/images/wait.gif" id="progressImg_{{ comm.id }}" alt="Nahrávám graf"/></p>
			</div>
		{% endif %}
	{% endfor %}
	</div>
{% endif %}

{% if mpuGroups|length %}
	<h2><a	href="#void_mpuGroups"
			id="sliderCtrl_mpus"
		>
			{% trans "Grafy podle skupin MPU" %}
		</a>
	</h2>
	<div id="mpu-accordion">
	{% for mpu in mpuGroups %}
		{% if mpu.stat %}
			<h3><a	href="#void_mpuGraphs"
					id="pieCtrl_{{ mpu.id }}"
					onClick="showPie(	'{{ mpu.id }}',
										'{% url get_group_poll_json mpu.id, poll.id %}',
										'{% url get_polls_in_group mpu.id, poll.id %}',
										'{{ mpu.name }}',
										createGroupGrid
									)"
				>
					{{ mpu.name }} <span class="verdict">[ {{ mpu.stat.getResult }} ]</span>
				</a>
			</h3>
			<div id="pie_{{ mpu.id }}" style="display: none;">
				<a href="{{ baseUrl }}?mainAccordIndex=4&clickElem=pieCtrl_{{ mpu.id }}">link</a>
				<p><img src="{{ MEDIA_URL }}/images/wait.gif" id="progressImg_{{ mpu.id }}" alt="Nahrávám graf"/></p>
			</div>
		{% endif %}
	{% endfor %}
	</div>
{% endif %}

{% if rest|length %}
	<h2><a	href="#void_Others"
			id="sliderCtrl_rest"
		>
			{% trans "Grafy podle ostatních skupin" %}
		</a>
	</h2>
	<div id="other-accordion">
	{% for group in rest %}
		{% if group.stat %}
			<h3><a	href="#void_OtherGraphs"
					id="pieCtrl_{{ group.id }}"
					onClick="showPie(	'{{ group.id }}',
										'{% url get_group_poll_json group.id, poll.id %}',
										'{% url get_polls_in_group group.id, poll.id %}',
										'{{ group.name }}',
										createGroupGrid
									)"
				>
					{{ group.name }} <span class="verdict">[ {{ group.stat.getResult }} ]</span>
				</a>
			</h3>
			<div id="pie_{{ group.id }}" style="display: none;">
				<a href="{{ baseUrl }}?mainAccordIndex=5&clickElem=pieCtrl_{{ group.id }}">link</a>
				<p><img src="{{ MEDIA_URL }}/images/wait.gif" id="progressImg_{{ group.id }}" alt="Nahrávám graf"/></p>
			</div>
		{% endif %}
	{% endfor %}
	</div>
{% endif %}

</div>

{% endblock %}
